import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'

Page({
  data: {
    imgIp: getApp().globalData.imgIp,
    currentTab: 0,
    exchangeList: [],
    usedList: [],
    userId: '',
    ticketInfo: null,
    authPanelHidden1: true,
    authPanelHidden2: true,
  },
  switchNav(e) {
    var that = this
    that.setData({
      currentTab: parseInt(e.currentTarget.dataset.current),
    })
  },

  swiperChange(e) {
    this.setData({
      currentTab: parseInt(e.detail.current),
    })
  },

  toQRcode(e) {
    wx.navigateTo({
      url:
        '/pages/qrcode/qrcode?item=' +
        JSON.stringify(e.currentTarget.dataset.item),
    })
  },
  // 删除记录
  del() {
    var that = this
    // that.setData({
    //   list: []
    // })
    wx.request({
      url: getApp().globalData.ip + '/exchangegoods/delete',
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      data: {
        user: getApp().globalData.userId,
      },
      method: 'DELETE',
      type: 'json',
      success: function (res) {
        if (res.data.code === 888) {
          // wx.showToast({
          //   title: '暂无记录可清除呢',
          //   icon: 'none',
          //   duration: 1000,
          // })
          Toast('暂无记录可清除呢')
        } else {
          // wx.showToast({
          //   title:'清空记录成功',
          //   icon: 'success',
          //   duration: 1000,
          // })
          Toast.success('清空记录成功')
          that.setData({
            usedList: [],
          })
        }
      },
    })
  },
  // 已兑换
  getExchangeList() {
    let that = this
    wx.request({
      url: getApp().globalData.ip + '/exchangegoods/queryexchangelist',
      header: {
        'content-type': 'application/json;charest=utf-8',
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      success: function (res) {
        console.log('已兑换:', res)
        if (res.data.code === 888) {
          that.setData({ authPanelHidden1: false })
        }
        if (res.data.data) {
          that.setData({
            exchangeList: res.data.data.reverse() || [],
          })
        }
      },
    })
  },
  // 已使用
  getUsedList() {
    let that = this
    wx.request({
      url: getApp().globalData.ip + '/exchangegoods/usedlist',
      header: {
        'content-type': 'application/json;charest=utf-8',
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
      },
      method: 'GET',
      success: function (res) {
        if (res.data.code === 888) {
          that.setData({ authPanelHidden2: false })
        }
        console.log('已使用:', res)
        if (res.data.data) {
          that.setData({
            usedList: res.data.data.reverse() || [],
          })
        }
      },
    })
  },

  onLoad: function (options) {
    var that = this
    that.getExchangeList()
    that.getUsedList()
  },

  onShow() {
    this.onLoad()
  },
})
